<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>我来学Kotlin-基础之包 - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="包对于模块化是非常重要的，Kotlin的包与Java的定义/语法类似，但是限制完全不同。 一个源文件的包定义应该放在代码开始处： 1 2 3 4 5 6 7" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E5%9F%BA%E7%A1%80%E4%B9%8B%E5%8C%85/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="我来学Kotlin-基础之包" />
<meta property="og:description" content="包对于模块化是非常重要的，Kotlin的包与Java的定义/语法类似，但是限制完全不同。 一个源文件的包定义应该放在代码开始处： 1 2 3 4 5 6 7" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E5%9F%BA%E7%A1%80%E4%B9%8B%E5%8C%85/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2017-06-14T17:43:44+00:00" />
<meta property="article:modified_time" content="2017-06-14T17:43:44+00:00" />

<meta itemprop="name" content="我来学Kotlin-基础之包">
<meta itemprop="description" content="包对于模块化是非常重要的，Kotlin的包与Java的定义/语法类似，但是限制完全不同。 一个源文件的包定义应该放在代码开始处： 1 2 3 4 5 6 7"><meta itemprop="datePublished" content="2017-06-14T17:43:44+00:00" />
<meta itemprop="dateModified" content="2017-06-14T17:43:44+00:00" />
<meta itemprop="wordCount" content="609">
<meta itemprop="keywords" content="Kotlin,Kotlin教程," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="我来学Kotlin-基础之包"/>
<meta name="twitter:description" content="包对于模块化是非常重要的，Kotlin的包与Java的定义/语法类似，但是限制完全不同。 一个源文件的包定义应该放在代码开始处： 1 2 3 4 5 6 7"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">我来学Kotlin-基础之包</h1>

      <div class="post-meta">
        <span class="post-time"> 2017-06-14 </span>
        <div class="post-category">
            <a href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/"> 编程语言 </a>
            </div>
          <span class="more-meta"> 约 609 字 </span>
          <span class="more-meta"> 预计阅读 2 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#默认引入">默认引入</a></li>
    <li><a href="#引入-imports">引入 (Imports)</a></li>
    <li><a href="#顶层定义可见性">顶层定义可见性</a></li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>包对于模块化是非常重要的，Kotlin的包与Java的定义/语法类似，但是限制完全不同。</p>
<!-- raw HTML omitted -->
<p>一个源文件的包定义应该放在代码开始处：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">package</span> <span class="nn">foo.bar</span>

<span class="k">fun</span> <span class="nf">baz</span><span class="p">()</span> <span class="p">{}</span>

<span class="k">class</span> <span class="nc">Goo</span> <span class="p">{}</span>

<span class="c1">// ...
</span></code></pre></td></tr></table>
</div>
</div><p>所有的内容被包含在此包的定义下面，在上面的例子中，<code>baz()</code>的全名是<code>foo.bar.bax</code>，<code>Goo</code>的全名是<code>foo.bar.Goo</code>。如果没有指定包名，那么些文件的代码属于默认没有名字的包。</p>
<h2 id="默认引入">默认引入</h2>
<p>有一些包被默认引入到每一个源文件中，不需要再另外显示的引入：</p>
<ul>
<li>kotlin.*</li>
<li>kotlin.annotation.*</li>
<li>kotlin.collections.*</li>
<li>kotlin.comparisons.* (从1.1开始)</li>
<li>kotlin.io.*</li>
<li>kotlin.ranges.*</li>
<li>kotlin.sequences.*</li>
<li>kotlin.text.*</li>
</ul>
<p>根据不同的平台还会默认引入其他的包：</p>
<ul>
<li>JVM:
<ul>
<li>java.lang.*</li>
<li>kotlin.jvm.*</li>
</ul>
</li>
<li>JS:
<ul>
<li>kotlin.js.*</li>
</ul>
</li>
</ul>
<h2 id="引入-imports">引入 (Imports)</h2>
<p>除了默认引入的一些包以外，每个文件应该包含自己的引入指令。</p>
<p>可以引入一个单独的名称，如：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">import</span> <span class="nn">foo.Bar</span> <span class="c1">// Bar可以直接使用，而不需要前面的前缀foo
</span></code></pre></td></tr></table>
</div>
</div><p>或者一个包下面的所有内容（包，类，对象等等）：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">import</span> <span class="nn">foo.*</span> <span class="c1">// foo下面的所有内容都可以直接访问
</span></code></pre></td></tr></table>
</div>
</div><p>如果有名称冲突了，可以使用<code>as</code>关键字在文件范围内重命名来消除冲突：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">import</span> <span class="nn">foo.Bar</span> <span class="c1">// Bar可以直接访问
</span><span class="c1"></span><span class="k">import</span> <span class="nn">bar.Bar</span> <span class="k">as</span> <span class="n">bBar</span> <span class="c1">// 使用bBar代替&#39;bar.Bar&#39;
</span></code></pre></td></tr></table>
</div>
</div><p>关键字<code>import</code>不局限于引入类，还可以引入其他的定义/声明：</p>
<ul>
<li>顶层（包级别）的函数和属性</li>
<li>单例对象内定义的方法和属性</li>
<li>枚举常量</li>
</ul>
<p>跟Java不同的一点是，Kotlin没有<code>import static</code>语法，所有的定义都是通过<code>import</code>关键字实现</p>
<h2 id="顶层定义可见性">顶层定义可见性</h2>
<p>如果声明/定义被标记为了<code>private</code>，那么这个声明/定义仅仅对当前文件可见，其他包不能引用。</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2017-06-14
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/kotlin/">Kotlin</a>
          <a href="/tags/kotlin%E6%95%99%E7%A8%8B/">Kotlin教程</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E5%9F%BA%E7%A1%80%E4%B9%8B%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">我来学Kotlin-基础之流程控制</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E5%9F%BA%E7%A1%80%E4%B9%8B%E5%9F%BA%E7%A1%80%E7%B1%BB%E5%9E%8B/">
            <span class="next-text nav-default">我来学Kotlin-基础之基础类型</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
